package com.sdnu.iosclub.staqvs.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdnu.iosclub.qvs.entity.*;
import com.sdnu.iosclub.qvs.entity.vo.QvsCurPeoSurveyVo;
import com.sdnu.iosclub.qvs.entity.vo.QvsSurveyVo;
import com.sdnu.iosclub.qvs.entity.vo.QvsVoterVo;
import com.sdnu.iosclub.qvs.mapper.QvsResultMapper;
import com.sdnu.iosclub.qvs.service.*;
import com.sdnu.iosclub.staqvs.service.QvsStaService;
import org.apache.commons.collections4.Put;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author li
 * @since 2022-09-16
 */
@Service
public class QvsStaServiceImpl implements QvsStaService {

    @Autowired
    QvsSurveyService qvsSurveyService;
    @Autowired
    QvsQuestionService qvsQuestionService;
    @Autowired
    QvsTextService qvsTextService;
    @Autowired
    QvsOptionService qvsOptionService;
    @Autowired
    QvsResultService qvsResultService;

    @Override
    public List<QvsQuestion> getAllQuestionBySurveyId(String id) {
        QueryWrapper<QvsQuestion> wrapper = new QueryWrapper<>();
        wrapper.eq("survey_id",id);
        wrapper.orderByAsc("orderby");
        List<QvsQuestion> questionList = qvsQuestionService.list(wrapper);
        return questionList;
    }

    @Override
    public List<QvsText> getAllTextByQuestionId(String id) {
        QueryWrapper<QvsText> wrapper = new QueryWrapper<>();
        wrapper.eq("question_id",id);
        List<QvsText> textList =qvsTextService.list(wrapper);
        return textList;
    }

    @Override
    public Map<String, Object> getAllOptionAndCountByQuestionId(String id) {
        HashMap<String, Object> map = new HashMap<>();

        QueryWrapper<QvsOption> wrapper = new QueryWrapper<>();
        wrapper.eq("question_id",id);
        wrapper.orderByAsc("orderby");
        List<QvsOption> optionList =qvsOptionService.list(wrapper);// 根据问题id获取到所有的选项

        ArrayList<String> list1 = new ArrayList<>();
        ArrayList<Integer> list2 = new ArrayList<>();

        for(int i=0;i<optionList.size();i++){
            QvsOption qvsOption = optionList.get(i);
            list1.add(qvsOption.getOptionContent());
            // 下面是根据选项的id查询选择人数
            String optionId=qvsOption.getId();
            QueryWrapper<QvsResult> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("option_id",optionId);
            list2.add(qvsResultService.count(queryWrapper));
        }
        map.put("optionContent",list1);
        map.put("count",list2);
        return map;
    }

    @Override
    public Map<String,Object> pageGetSurvey(Long current, Long size, QvsSurveyVo surveyVo) {
        Page<QvsSurvey> page=new Page<>(current,size);

        QueryWrapper<QvsSurvey> wrapper=new QueryWrapper<>();

        String id=surveyVo.getId();
        String title=surveyVo.getTitle();
        Date startTime=surveyVo.getStartTime();
        Date endTime=surveyVo.getEndTime();
        Integer rules=surveyVo.getRules();
        Integer state=surveyVo.getState();
        Integer anon=surveyVo.getAnon();
        String creator=surveyVo.getCreator();

        if(!StringUtils.isEmpty(id)) {
            wrapper.like("id", id);
        }
        if(!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        if(!(startTime ==null)){
            wrapper.ge("start_time",startTime);
        }
        if(!(endTime==null)){
            wrapper.le("end_time",endTime);
        }
        if(!(rules==null)){
            wrapper.eq("rules",rules);
        }
        if(!(state==null)){
            wrapper.eq("state",state);
        }
        if(!(anon==null)){
            wrapper.eq("anon",anon);
        }
        if(!StringUtils.isEmpty(creator)){
            wrapper.like("creator",creator);
        }
        wrapper.orderByDesc("start_time");
        qvsSurveyService.page(page,wrapper);

        long total = page.getTotal();//总记录数
        List<QvsSurvey> list = page.getRecords(); //数据list集合

        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("list",list);

        return map;
    }

    @Override
    public Map<String,Object> pageGetQuestion(String surveyId,Long current, Long size, QvsQuestion qvsQuestion) {
        Page<QvsQuestion> page=new Page<>(current,size);

        QueryWrapper<QvsQuestion> wrapper=new QueryWrapper<>();
        wrapper.eq("survey_id",surveyId);

        String id=qvsQuestion.getId();
        String title=qvsQuestion.getTitle();
        Integer orderby = qvsQuestion.getOrderby();
        Integer required = qvsQuestion.getRequired();
        Integer type = qvsQuestion.getType();
        Date createTime = qvsQuestion.getCreateTime();


        if(!StringUtils.isEmpty(id)) {
            wrapper.eq("id", id);
        }
        if(!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        if(!(orderby==null)){
            wrapper.eq("orderby",orderby);
        }
        if(!(required==null)){
            wrapper.eq("required",required);
        }
        if(!(type==null)){
            wrapper.eq("type",type);
        }
        if(!(createTime==null)) {
            wrapper.ge("create_time", createTime);
        }

        wrapper.orderByAsc("orderby");
        qvsQuestionService.page(page,wrapper);

        long total = page.getTotal();//总记录数
        List<QvsQuestion> list = page.getRecords(); //数据list集合

        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("list",list);

        return map;
    }

    @Override
    public Map<String, Object> getVoterByQuestionId(String id) {
        HashMap<String, Object> map = new HashMap<>();

        // 查找所有的option和text
        QueryWrapper<QvsResult> resultWrapper = new QueryWrapper<>();
        QueryWrapper<QvsText> textWrapper = new QueryWrapper<>();
        resultWrapper.eq("question_id",id);
        textWrapper.eq("question_id",id);
        List<QvsResult> resultList = qvsResultService.list(resultWrapper);
        List<QvsText> textList = qvsTextService.list(textWrapper);

        // 根据voter从视图中查找填写人信息
        List<QvsVoterVo> resultVoter=new ArrayList<>();
        List<QvsVoterVo> textVoter=new ArrayList<>();
        for(int i=0;i<resultList.size();i++){
            String voterId = resultList.get(i).getVoter();
            QvsVoterVo voter = qvsResultService.getVoterById(voterId);

            // 获得选项内容
            String optionId = resultList.get(i).getOptionId();
            QueryWrapper<QvsOption> wrapper = new QueryWrapper<>();
            wrapper.eq("id",optionId);
            QvsOption option = qvsOptionService.getOne(wrapper);

            voter.setInfo(option.getOptionContent());
            resultVoter.add(voter);
        }
        for(int i=0;i<textList.size();i++){
            String voterId = textList.get(i).getVoter();
            QvsVoterVo voterVo = qvsTextService.getVoterById(voterId);
            voterVo.setInfo(textList.get(i).getResult());
            textVoter.add(voterVo);
        }
        map.put("resultVoter",resultVoter);
        map.put("textVoter",textVoter);
        map.put("resultCount",resultVoter.size());
        map.put("textCount",textVoter.size());
        return map;
    }

    @Override
    public List<QvsVoterVo> getVoterBySurveyId(String id,QvsVoterVo qvsVoterVo) {

        // 根据问卷id查找到所有问题
        QueryWrapper<QvsQuestion> questionWrapper = new QueryWrapper<>();
        questionWrapper.eq("survey_id",id);
        List<QvsQuestion> questionList = qvsQuestionService.list(questionWrapper);

        // 根据问题id查找到所有result和text
        List<QvsResult> resultList = new ArrayList<>();
        List<QvsText> textList = new ArrayList<>();
        for(int i=0;i<questionList.size();i++){
            String questionId=questionList.get(i).getId();
            textList.addAll(getAllTextByQuestionId(questionId));

            QueryWrapper<QvsResult> resultWrapper = new QueryWrapper<>();
            questionWrapper.eq("question_id",questionId);
            resultList.addAll(qvsResultService.list(resultWrapper));
        }
        // 将所有的voterId放入set中
        Set<String> set = new HashSet<>();
        for(int i=0;i<resultList.size();i++){
            set.add(resultList.get(i).getVoter());
        }
        for(int i=0;i<textList.size();i++){
            set.add(textList.get(i).getVoter());
        }

        // 从视图查找所有的voter信息
        List<QvsVoterVo> resList =qvsQuestionService.getVotersByIdSet(set,qvsVoterVo);

        return resList;
    }

    @Override
    public List<QvsCurPeoSurveyVo> getAllInfoByVoterIdAndSurveyId(String surveyId, String voterId) {
        List<QvsCurPeoSurveyVo> resList=new ArrayList<>();

        // 先查找到所有的问题
        QueryWrapper<QvsQuestion> questionWrapper = new QueryWrapper<>();
        questionWrapper.eq("survey_id",surveyId);
        questionWrapper.orderByAsc("orderby");
        List<QvsQuestion> questionList = qvsQuestionService.list(questionWrapper);

        // 根据问题查找此人的作答结果
        for(int i=0;i<questionList.size();i++){
            QvsCurPeoSurveyVo vo = new QvsCurPeoSurveyVo();
            QvsQuestion question = questionList.get(i);
            vo.setTitle(question.getTitle());
            vo.setOrderby(question.getOrderby());
            vo.setType(question.getType());

            if(question.getType()==3 || question.getType()==4){
                // 查找text的结果
                QueryWrapper<QvsText> textWrapper = new QueryWrapper<>();
                textWrapper.eq("question_id",question.getId());
                textWrapper.eq("voter",voterId);
                QvsText text = qvsTextService.getOne(textWrapper);
                if(text!=null){
                    vo.setResult(text.getResult());
                }else{
                    vo.setResult("未作答");
                }

            }else{
                // 查找result的结果
                QueryWrapper<QvsResult> resultWrapper = new QueryWrapper<>();
                resultWrapper.eq("question_id",question.getId());
                resultWrapper.eq("voter",voterId);
                QvsResult result = qvsResultService.getOne(resultWrapper);

                if(result!=null){
                    String optionId=result.getOptionId();
                    QueryWrapper<QvsOption> optionWrapper = new QueryWrapper<>();
                    optionWrapper.eq("id",optionId);
                    QvsOption option = qvsOptionService.getOne(optionWrapper);
                    vo.setResult(option.getOptionContent());
                }else{
                    vo.setResult("未作答");
                }

            }
            resList.add(vo);
        }
        return resList;
    }


}
